package com.ericsson.entironment.sms;

import java.io.IOException;
import java.util.HashMap;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ericsson.common.xml.XMLException;
import com.huawei.insa2.comm.cmpp.message.CMPPMessage;
import com.huawei.insa2.comm.cmpp.message.CMPPSubmitMessage;
import com.huawei.insa2.comm.cmpp.message.CMPPSubmitRepMessage;
import com.huawei.insa2.util.Args;
import com.huawei.insa2.util.Cfg;
import com.huawei.smproxy.SMProxy;


/**
 * 
 * <p>
 * Title: WSSEDSMP
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: GentleSoft
 * </p>
 * 
 * <p>
 * Description: 短信发送进程
 * </p>
 * 
 * @author Oarsman
 * @version 1.0 Jul 4, 2009
 */
public class SMSHandler
{

	private boolean isContinue=true;
	private static SMSHandler handler=null;	
	private static Log log=LogFactory.getLog(SMSHandler.class);
	private SMProxy smsSender;
	private Args args=null;
	private  SMSHandler()
	{
		int i;
		Connection connection = null;
		log.info("\n-----正在启动短信队列处理程序-----\n");
		try
		{
			
			//初始化短信代理
			String path=SMSHandler.class.getResource("/SMProxy.xml").getPath();
			if (path.indexOf(":")!=-1)
				path=path.substring(1,path.length());
			args=new Cfg(path).getArgs("ismg");
			smsSender=new SMProxy(args);

		}
		catch (Exception e)
		{	
			e.printStackTrace();
			log.error(e);
		}
		log.info("\n-----短信队列处理程序启动成功-----\n");
	}
	
	public static synchronized SMSHandler getInstance()
	{
		if (handler==null)
		{
			handler=new SMSHandler();
		}

		return handler;

	}


	public void sendMessage(String message,String[] target)
	{
		CMPPSubmitMessage smsmsg=null;
		MsgInfo config = null;

			
		//获取短信分发参数
		try
		{
			config=MsgInfo.getInstance();
		}
		catch (XMLException e)
		{
			log.error("\n短信发送失败,无法读取短信发送配置信息\n",e);
		}
		try
		{
			smsmsg=new CMPPSubmitMessage(
					1,
					1,
					config.registeredDelivery, 
					config.msgLevel,
					config.serviceId,
					config.feeUserType, 
					config.feeTerminalId,
					config.tpPid,
					config.tpUdhi,
					4,
					config.msgSrc,
					config.feeType,
					config.feeCode,
					config.validTime,
					config.atTime,
					config.srcId,
					target,
					message.getBytes(),
					""
			);
			
			CMPPSubmitRepMessage  revalue=(CMPPSubmitRepMessage) smsSender.send(smsmsg);
			
			if (revalue.getResult()==0)
			{//消息发送成功
				if (log.isDebugEnabled())
					log.debug("\n成功发送一条短信["+message+"]");
			}
			else
			{
				log.error("\n短信发送失败，失败代码["+revalue.getResult()+"]--\n");
	
			}
		}
		catch (IllegalArgumentException e)
		{
			log.error("\n短信发送失败,无法获得短信内容\n");
			log.error(e);
		}

		catch (IOException e)
		{
			log.error("\n短信发送失败,输入输出错误\n");
			log.error(e);
		}
		
		

	}

}
